package bby.app.fettool;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class Main extends JFrame implements ActionListener {
    JPanel panel;
    JTextField text;
    JMenuBar bar;
    JMenu menu;
    JMenuItem itemInitial , itemChange;
    FlowLayout layout = new FlowLayout();
    SenderPage senderPage;
    ReceiverPage receiverPage;
    MyManager myManager;

    public void init(){
        bar = new JMenuBar();
        menu = new JMenu("菜单");
        itemInitial = new JMenuItem("接收文件");
        itemChange = new JMenuItem("发送文件");
        panel = new JPanel();
        text = new JTextField(10);
        senderPage = new SenderPage();
        receiverPage = new ReceiverPage();

        text.setText("这里是主界面，请在左上角选择进入相关界面！");
        text.setEditable(false);
        itemInitial.addActionListener(this);
        itemChange.addActionListener(this);
        bar.add(menu);
        menu.add(itemInitial);
        menu.add(itemChange);
        panel.setLayout(layout);
        add(panel);
        panel.add(text);
        setJMenuBar(bar);

        try {
            myManager = new MyManager(receiverPage);
//            myManager.start();
            receiverPage.setMyManager(myManager);
        } catch (Exception e) {
            e.printStackTrace();
            receiverPage.print("程序文件配置出错！");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == itemChange){//改变图形化界面为“后来”的界面。
            System.out.println("发送文件");
            menu.setText("发送文件");
            panel.removeAll();
            panel.add("发送文件解密" , senderPage);//切换代码。
            panel.validate();
            repaint();
        }
        else if(e.getSource() == itemInitial){//改变图形化界面为“初始”界面。
            menu.setText("接收文件");
            System.out.println("接收文件");
            panel.removeAll();
            panel.add("接收文件界面" , receiverPage);
            panel.validate();
            repaint();
            myManager.flashFileList();
            myManager.loadKeypair();
        }
    }

    void showWindow(){
        setBounds(500,300,800,600);
        setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}
